/**
 * @file   Float32.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-19
 * @brief  DON data type object: Float32
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/DataType/Float32.h>
#include <DON/Core/DataType/UInt32.h>
#include <DON/Core/Platform/PlatformDefines.h>
#include <QtCore/QByteArray>

using namespace DON::Core::DataType;

class Float32::Private
{
public:
    Private()
        : value(0.0f)
    { }

    float value;
};

Float32::Float32()
    : AbstractDataType(),
      d(new Private())
{
}

Float32::Float32(const Float32 &other)
    : AbstractDataType(),
      d(new Private())
{
    d->value = other.d->value;
}

Float32::~Float32()
{
    delete d;
}

Float32 & Float32::operator=(const Float32 &other)
{
    d->value = other.d->value;

    return *this;
}

AbstractDataType * Float32::clone() const
{
    return new Float32(*this);
}

DataType Float32::getDataType() const
{
    return DataType_Float32;
}

float Float32::getValue() const
{
    return d->value;
}

void Float32::setValue(const float value)
{
    d->value = value;
}

bool Float32::fromBinary(const QByteArray &data, const int startIndex, int *size)
{
    bool success = false;
    float value = Float32::fromBinary(data, startIndex, &success);

    if (success)
    {
        d->value = value;

        if (size != NULL)
        {
            *size = 4;
        }
    }

    return success;
}

QByteArray Float32::toBinary() const
{
    return Float32::toBinary(d->value);
}

Float32 * Float32::fromAbstractDataType(AbstractDataType *instance)
{
    Float32 *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_Float32)
        {
            outputInstance = static_cast<Float32 *>(instance);
        }
    }

    return outputInstance;
}

float Float32::fromBinary(const QByteArray &data, const int startIndex, bool *ok)
{
    bool success = false;
    float value = 0.0f;

#if (DON_CORE_PLATFORM_ENDIANESS_FLOAT32 == DON_CORE_PLATFORM_LITTLE_ENDIAN) || \
    (DON_CORE_PLATFORM_ENDIANESS_FLOAT32 == DON_CORE_PLATFORM_BIG_ENDIAN)
    union
    {
        quint32 valueUInt32;
        float valueFloat32;
    } converter;

    converter.valueUInt32 = UInt32::fromBinary(data, startIndex, &success);

    if (success)
    {
        value = converter.valueFloat32;
    }
#else
#  error "Unknown FLOAT32 endianess!"
#endif

    if (ok != NULL)
    {
        *ok = success;
    }

    return value;
}

QByteArray Float32::toBinary(const float value)
{
    quint32 rawValue = 0u;

#if (DON_CORE_PLATFORM_ENDIANESS_FLOAT32 == DON_CORE_PLATFORM_LITTLE_ENDIAN) || \
    (DON_CORE_PLATFORM_ENDIANESS_FLOAT32 == DON_CORE_PLATFORM_BIG_ENDIAN)
    union
    {
        quint32 valueUInt32;
        float valueFloat32;
    } converter;

    converter.valueFloat32 = value;
    rawValue = converter.valueUInt32;
#else
#  error "Unknown FLOAT32 endianess!"
#endif

    return UInt32::toBinary(rawValue);
}
